package com.sicau.smarttraceplatform.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.sicau.smarttraceplatform.common.aop.ValidateAspect;
import com.sicau.smarttraceplatform.common.constants.CommonURLConstants;
import com.sicau.smarttraceplatform.common.returning.ResultData;
import com.sicau.smarttraceplatform.entity.Good;
import com.sicau.smarttraceplatform.entity.Trace;
import com.sicau.smarttraceplatform.entity.User;
import com.sicau.smarttraceplatform.entity.param.good.GoodAddParam;
import com.sicau.smarttraceplatform.entity.param.good.GoodQueryParam;
import com.sicau.smarttraceplatform.entity.param.good.GoodUpdateParam;
import com.sicau.smarttraceplatform.entity.vo.PageDataVO;
import com.sicau.smarttraceplatform.service.GoodService;
import com.sicau.smarttraceplatform.service.TraceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.dozer.Mapper;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.time.LocalDateTime;
import java.util.Arrays;

import static com.sicau.smarttraceplatform.common.constants.commonConstants.CURRENT_USER;

/**
 * <p>
 * 商品信息 前端控制器
 * </p>
 *
 * @author sxt && ttxxi
 * @since 2020-05-24
 */
@RestController
@RequestMapping(CommonURLConstants.GOOD_PREFIX)
@Api(tags = "商品信息")
public class GoodController {

    @Resource
    private Mapper dozerMapper;
    @Resource
    private GoodService goodService;
    @Resource
    private TraceService traceService;

    @GetMapping()
    @ApiOperation("根据条件查询，不传条件查询全部")
    public ResultData getGood(GoodQueryParam goodQueryParam) {
        PageDataVO page = goodService.queryGoodByCondition(goodQueryParam);
        return new ResultData<>().OK().setObj(page);
    }


    @DeleteMapping()
    @ApiOperation("根据id删除")
    public ResultData getById(String[] ids) {
        goodService.removeByIds(Arrays.asList(ids));
        return new ResultData<>().OK();
    }

    @PostMapping()
    @ApiOperation("添加商品")
    @ValidateAspect.ParamValidate()
    public ResultData addArea(@ApiParam(hidden = true) HttpServletRequest request,
                              @Valid GoodAddParam goodAddParam,
                              @ApiParam(hidden = true) BindingResult bindingResult) {
        User user = (User) request.getSession().getAttribute(CURRENT_USER);
        Trace trace = traceService.getOne(new LambdaQueryWrapper<Trace>().eq(Trace::getTraceId, goodAddParam.getTraceId()));
        if (trace == null){
            return new ResultData().FAILED().setResultIns("traceId错误");
        }
        Good good = dozerMapper.map(goodAddParam, Good.class);
        good.setCreator(user!=null?user.getId().toString():"")
            .setCreated(LocalDateTime.now());
        goodService.save(good);
        return new ResultData().OK();
    }

    @PutMapping()
    @ApiOperation("根据id更新")
    public ResultData deleteById(@ApiParam(hidden = true) HttpServletRequest request,
                                 @Valid GoodUpdateParam goodUpdateParam,
                                 @ApiParam(hidden = true) BindingResult bindingResult) {
        User user = (User) request.getSession().getAttribute(CURRENT_USER);
        Good good = dozerMapper.map(goodUpdateParam, Good.class)
                            .setMender(user!=null?user.getId().toString():"")
                            .setModified(LocalDateTime.now());
        goodService.updateById(good);
        return new ResultData().OK();
    }
}

